package org.scc.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.scc.exception.SCCException;
import org.scc.model.User;
import org.scc.service.UserService;
import org.scc.vo.PageVO;

import com.opensymphony.xwork2.ActionContext;

public class UserAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	private User user;

	private UserService userService;
	
	private String oldPwd;
	private String newPwd;
	
	public UserAction(){
		userService = new UserService();
	}
	
	/**
	 * 检查用户是否为空
	 */
	private void checkUser(){
		if(user == null){
			throw new SCCException("用户信息文空!");
		}
	}
	
	public String login(){
		
		checkUser();
		
		User loginUser = userService.login(user.getUsername(), user.getPassword());
		if(loginUser == null){
			return ERROR;
		} else {
			ActionContext context = ActionContext.getContext();
			Map<String, Object> session = context.getSession();
			session.put("user", loginUser);
			return SUCCESS;
		}
	}
	
	public String logout(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.remove("user");
		return SUCCESS;
	}
	
	public void addUser() throws IOException{
		if(user == null){
			throw new SCCException("请输入用户信息!");
		}
		user.setUsername( userService.getNextUsername() );
		user.setPassword( userService.getDefaultPassword() );
		user.setStatus(User.IN);
		userService.add(user);
		responseSuccess();
	}
	
	public String initEditPage(){
		user = userService.load(id);
		return SUCCESS;
	}
	
	public void updateUser(){
		checkUser();
		
		User oldUser = userService.load(user.getId() );
		oldUser.setName(user.getName() );
		oldUser.setQq(user.getQq());
		userService.update(oldUser);
		responseSuccess();
	}
	
	public void deleteUsers(){
		if(ids == null){
			throw new SCCException("您未选中任何用户!");
		}
		userService.delete(ids);
		responseSuccess();
	}
	
	public void listUsers(){
		PageVO<User> pageVO = userService.findPage(getRowBeginIndex(), rows);
		responeString( getPageJSON(pageVO) );
	}
	
	private String getPageJSON(PageVO<User> pageVO){
		JSONArray arr = new JSONArray();
		JSONObject obj;
		List<User> users = pageVO.getData();
		for(User user : users){
			obj = new JSONObject();
			obj.put("id", user.getId() );
			obj.put("name", user.getName() );
			obj.put("username", user.getUsername() );
			obj.put("qq", user.getQq() );
			obj.put("status", user.getStatusText() );
			arr.add(obj);
		}
		obj = new JSONObject();
		obj.put("total", pageVO.getTotal());
		obj.put("rows", arr);
		return obj.toString();
	}
	
	public void searchUser(){
		List<User> users = userService.searchUser(user.getName());
		responeString(getUsersJSON(users));
	}
	
	private String getUsersJSON(List<User> users){
		JSONArray arr = new JSONArray();
		JSONObject obj = null;
		for(User user : users){
			obj = new JSONObject();
			obj.put("id", user.getId());
			obj.put("name", user.getName());
			arr.add(obj);
		}
		return arr.toString();
	}
	
	public void findNoGroupUser(){
		List<User> users = userService.findNoGroupUsers();
		responeString(getUsersJSON(users));
	}
	
	public void findUsersIn(){
		List<User> users = userService.findUserIn();
		responeString(getUsersJSON(users));
	}
	
	public void changePassword(){
		User cu = getCurrentUser();
		if( cu.getPassword().equals( userService.getEncryptPassword(oldPwd) ) ){
			cu.setPassword( userService.getEncryptPassword(newPwd) );
			userService.update(cu);
			responeString("修改密码成功,请牢记您的新密码！");
		} else {
			responeString("修改密码失败！");
		}
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}
	
}
